<?php
/**
 * Created by PhpStorm.
 * User: ChengChaogui
 * Date: 2016/11/29
 * Time: 14:29
 */

namespace App\Framework\Helper;


class TimeHelper
{
    /**
     * 获取今天的开始时间
     * @return false|int
     */
    public static function getTodayStart()
    {
        $now = time();
        $date = mktime(0, 0, 0, date('m', $now), date('d', $now), date('y', $now));
        return date('Y-m-d H:i:s', $date);
    }

    /**
     * 获取今天的结束时间
     * @return false|int
     */
    public static function getTodayEnd()
    {
        $now = time();
        $date = mktime(59, 59, 59, date('m', $now), date('d', $now), date('y', $now));
        return date('Y-m-d H:i:s', $date);
    }

    /**
     * 将时间字符串转换为开始时间
     * @param $str
     * @return false|int
     */
    public static function getStartTime($str)
    {
        $time = strtotime($str);
        $date = mktime(0, 0, 0, date('m', $time), date('d', $time), date('y', $time));
        return date('Y-m-d H:i:s', $date);
    }

    /**
     * 将时间字符串转换为结束时间
     * @param $str
     * @return false|int
     */
    public static function getEndTime($str)
    {
        $time = strtotime($str);
        $date = mktime(23, 59, 59, date('m', $time), date('d', $time), date('y', $time));
        return date('Y-m-d H:i:s', $date);
    }

    /**
     * 将日期和时间拼接成完整的datetime格式
     * @param $dateStr
     * @param $timeStr
     * @return false|null|string
     */
    public static function jointDateTime($dateStr, $timeStr)
    {
        $str = $dateStr . ' ' . $timeStr;
        $datetime = strtotime($str);
        if ($datetime == false) {
            return null;
        }

        return date('Y-m-d H:i:s', $datetime);
    }

    /**
     * 将日期字符串转换为mysql日期格式
     * @return false|string
     */
    public static function getNow()
    {
        $date = date('Y-m-d H:i:s', time());
        return $date;
    }

    /**
     * 获取精确到毫秒的时间戳
     * @return float
     */
    public static function getMicroTimestamp(){
        list($msec,$sec) = explode(' ',microtime());
        return (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
    }
}